package pro.softzhang.algo.lc800;

import pro.softzhang.algo.flag.book.BDaily;
import pro.softzhang.algo.flag.difficulty.DRed;
import pro.softzhang.algo.flag.knowledge.KStack;

import java.util.Stack;

@DRed
@BDaily(20221009)
@KStack
public class LC856_ScoreOfParentheses {
    public static void main(String[] args) {

    }

    static
    class Solution {
        public int scoreOfParentheses(String s) {
            Stack<Integer> stack = new Stack<>();
            stack.push(0);
            for (char ch : s.toCharArray()) {
                if (ch == '(')
                    stack.push(0);
                else {
                    int score = stack.pop();
                    stack.push(stack.pop() + Math.max(2 * score, 1));
                }
            }
            return stack.peek();
        }
    }
}
